//
//  JCCHeaderView.m
//  test
//
//  Created by jcc on 16/6/16.
//  Copyright © 2016年 jcc. All rights reserved.
//

#import "JCCHeaderView.h"
#import "JCCSectionModel.h"

@interface JCCHeaderView ()

@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation JCCHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        CGFloat g = [UIScreen mainScreen].bounds.size.width;
        
        self.arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brand_expand"]];
        self.arrowImageView.frame = CGRectMake(10, (44 - 8) / 2, 15, 8);
        [self.contentView addSubview:self.arrowImageView];

        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, g, 44);
        [button addTarget:self action:@selector(onOpen:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 200, 44)];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.titleLabel];
        self.contentView.backgroundColor = [UIColor purpleColor];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 44 - 0.5, g, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:line];

    }
    return self;
}

- (void)setModel:(JCCSectionModel *)model{
    if (_model != model) {
        _model = model;
    }
    if (model.isOpen) {
        self.arrowImageView.transform = CGAffineTransformIdentity;
    }else{
        
        self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
    }
    
    self.titleLabel.text = model.sectionTitle;
    
}

- (void)onOpen:(UIButton *)sender{
    NSLog(@"%i",self.model.isOpen);
    self.model.isOpen = !self.model.isOpen;
    [UIView animateWithDuration:0.25 animations:^{
        if (self.model.isOpen) {
            self.arrowImageView.transform = CGAffineTransformIdentity;
            
        }else{
            self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
        }
    }];
    
    if (self.ExpandCallback) {
        self.ExpandCallback(self.model.isOpen);
    }
    
}
@end
